package
{
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import mx.containers.Canvas;
	import mx.controls.Image;
	import mx.core.BitmapAsset;
	import mx.events.DragEvent;

	public class MovableComponent extends Canvas
	{
		[Embed(source="move.png")]
		private var classMove:Class;
		
		private var allowDrag:Boolean = false;
		private var dragging:Boolean = false;

		private var imgMove:Image = new Image();
		//private var cnvResize:UIComponent = new UIComponent();

		public function MovableComponent()
		{
			super();
			
			imgMove.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			imgMove.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			imgMove.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			
			addEventListener(MouseEvent.MOUSE_MOVE, onThisMouseMove);
			
			//addEventListener(MouseEvent.CLICK, onThisClick);
			
			var bmpMove:BitmapAsset = new classMove() as BitmapAsset;
			imgMove.source = bmpMove;
			imgMove.useHandCursor = imgMove.buttonMode = true;
			imgMove.setStyle("horizontalCenter", 0);
			imgMove.setStyle("verticalCenter", 0);
			addChild(imgMove);
		}
		
		private function onThisMouseMove(e:MouseEvent):void{
			if(!e.buttonDown && dragging){
				stopDragging();
			}
		}

		private function onMouseDown(e:MouseEvent):void{
			allowDrag = true;
			selected = true;
		}

		private function onMouseMove(e:MouseEvent):void{
			if(e.buttonDown){
				if(allowDrag && !dragging){
					dispatchEvent(new Event(DragEvent.DRAG_START));
					startDrag(false, new Rectangle(0,0, parent.width - width, parent.height - height));
					dragging = true;
				}
				dispatchEvent(new Event("dragging"));
			}
			else if(dragging){
				stopDragging();
			}
		}
		
		private function onMouseUp(e:MouseEvent):void{
			if(dragging){
				stopDragging();
			}
		}
		
		private function stopDragging():void{
			dispatchEvent(new Event(DragEvent.DRAG_COMPLETE));
			stopDrag();
			dragging = false;
			allowDrag = false;
		}
		
		// overrides
		public override function addChild(child:DisplayObject):DisplayObject{
			if(numChildren < 1){
				super.addChild(child);
			}
			else{
				addChildAt(child, numChildren - 1)
			}
			return child;
		}
		
		// resizing
		private var _selected:Boolean = false;
		public function set selected(value:Boolean):void{
			if(value){
				dispatchEvent(new Event(Event.SELECT));
			}
			_selected = value;
		}
		public function get selected():Boolean{
			return _selected;
		}
		
		private function onThisClick(e:MouseEvent):void{
			selected = true;
		}
		
		protected override function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
			var g:Graphics = this.graphics;
			g.clear();
			g.lineStyle(0, 0xeeeeee);
			g.drawRect(0,0, unscaledWidth, unscaledHeight);
			
			super.updateDisplayList(unscaledWidth, unscaledHeight);
		}
	}
}